package com.gzk.controller;

import com.gzk.feignClients.ProductFeign;
import com.gzk.query.ProductStoreQuery;
import com.gzk.service.IProductStoreService;
import com.gzk.domain.ProductStore;
import com.gzk.hrm.util.AjaxResult;
import com.gzk.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@RestController
@RequestMapping("/store")
public class ProductStoreController {
    @Autowired
    public IProductStoreService productStoreService;

    @Autowired
    private ProductFeign productFeign;

    /**
     * 保存和修改公用的
     * @param productStore  传递的实体
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody ProductStore productStore){
        try {
            if(productStore.getId()!=null){
                productStoreService.updateById(productStore);
            }else{
                productStoreService.insert(productStore);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productStoreService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ProductStore get(@PathVariable("id")Long id)
    {
        return productStoreService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<ProductStore> list(){

        return productStoreService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<ProductStore> json(@RequestBody ProductStoreQuery query)
    {
        Page<ProductStore> page = new Page<ProductStore>(query.getPage(),query.getRows());
        page = productStoreService.selectPage(page);

        List<ProductStore> list = page.getRecords();
        System.out.println(list);
        //根据id查商品名
        list.stream().forEach(new Consumer<ProductStore>() {
            @Override
            public void accept(ProductStore p) {
                System.out.println(p);
                p.setProductName(productFeign.get(p.getProductId()).getName());
            }
        });

        return new PageList<ProductStore>(page.getTotal(),list);
    }

    /**
     * 根据商品id获取库存
     */
    @RequestMapping(value = "/getStore/{productId}",method = RequestMethod.GET)
    public Long getStore(@PathVariable("productId") Long id){
        return productStoreService.getStoreByProductId(id);
    }

    /**
     * 减库存
     */
    @RequestMapping(value = "/reduce",method = RequestMethod.POST)
    public Integer reduceStore(@RequestBody Map<String,Long> map){
        try {
            productStoreService.reduceStore(map);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

}
